<template>
  <div>
    <div class="order-dropdown-box">
      <span>时间：</span>
      <el-dropdown  @command="handleTimeCommand" trigger="click">
        <span class="xz-dropdown-link">
          {{selectTimeItem.name}}<svg-icon iconClass="triangle"/>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-for="item in timeList" :key="item.name" :command="item" :class="{active:selectTimeItem.name===item.name}">{{item.name}}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <span style="margin-left:20px;">分类：</span>
      <el-dropdown  trigger="click" @command="handleTypeCommand">
        <span class="xz-dropdown-link">
          {{selectTypeItem.name}}<svg-icon iconClass="triangle"/>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-for="item in typeList" :key="item.name" :command="item" :class="{active:selectTypeItem.name===item.name}">{{item.name}}</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-table :data="tableData"
      v-loading="listLoading"
      max-height="370px"
      class="order-table">
      <el-table-column
        header-align="center"
        prop="title"
        label="交易名称"
        min-width="150">
        <template slot-scope="scope">{{scope.row.title}}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="order_id"
        label="收支明细"
        min-width="90">
        <template slot-scope="scope">
          {{scope.row.cat===2||scope.row.cat===3?'-':'+'}}{{scope.row.amount}}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label-class-name="order-last-header"
        label="类型"
        width="60">
        <template slot-scope="scope">
          {{scope.row.cat|typeFilter}}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="余额"
        min-width="110"
        prop="a_amount">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="pay_price"
        label="时间"
        width="115">
        <template slot-scope="scope">
          {{scope.row.created_at|parseTime('{y}-{m}-{d} {h}:{i}')}}
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      small
      hide-on-single-page
      class="order-pagination"
      :pager-count="5"
      layout="prev, pager, next"
      :page-size="100"
      :current-page.sync="page"
      :total="total"
      @current-change="getList">
    </el-pagination>
  </div>
</template>
<script>
import { orderListApi } from '@/api/zb'
export default {
  name: 'OrderTable',
  props: {
    timeList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      listLoading: false,
      selectTimeItem: { name: '全部' },
      selectTypeItem: { name: '全部' },
      typeList: [
        { id: 24008, name: '全部', value: null },
        { id: 24009, name: '充值', value: 1 },
        { id: 24010, name: '消费', value: 2 }
      ],
      page: 1,
      total: 0,
      tableData: []
    }
  },
  filters: {
    typeFilter (val) {
      const arr = ['', '充值', '消费', '退款', '赠送', '返还']
      return arr[val]
    }
  },
  activated () {
    this.getList()
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      const params = {
        page: this.page,
        page_size: 100
      }
      if (this.selectTimeItem.begin_date) {
        params.begin_date = this.selectTimeItem.begin_date
        params.end_date = this.selectTimeItem.end_date
      }
      if (this.selectTypeItem.value) {
        params.cat = this.selectTypeItem.value
      }
      this.listLoading = true
      orderListApi(params).then(res => {
        const data = res.data || {}
        this.tableData = data.list
        this.total = data.count
      }).finally(() => {
        this.listLoading = false
      })
    },
    handleTimeCommand (command) {
      this.$xz('xz_substat_action', command.zbId)
      this.selectTimeItem = command
      this.page = 1
      this.getList()
    },
    handleTypeCommand (command) {
      this.$xz('xz_substat_action', command.id)
      this.selectTypeItem = command
      this.page = 1
      this.getList()
    }
  }
}
</script>
